<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/Opportunity for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Opportunity\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Opportunity\Model\Opportunity;
use Opportunity\Form\OpportunityForm;
use Opportunity\Form\DetailForm;

class OpportunityController extends AbstractActionController
{
	
	protected $opportunityTable;
	protected $detailTable;
	protected $homeTable;
	
    public function indexAction()
    {
    	
    	return new ViewModel(array(
             'opportunities' => $this->getHomeTable()->fetchAll(),
        ));
    }
    
    
    public function addAction()
    {
    	$form = new OpportunityForm();
    	$form->get('submit')->setValue('Save');
    	$request = $this->getRequest();
    
    	if($request->isPost())
    	{
    		$opportunity = new Opportunity();
    		$form->setInputFilter($opportunity->getInputFilter());
    		$form->setData($request->getPost());
    		//echo 'valid';
    		if($form->isValid())
    		{
    			echo 'valid';
    			$opportunity->exchangeArray($form->getData());
    			$this->getOpportunityTable()->saveOpportunity($opportunity);
    
    			return $this->redirect()->toRoute('opportunity');
    		}
    		//echo $opportunity->name;
    		$testing = $form->getData();
    		echo $testing['name'];
    
    	}return array('form' => $form);
    }
    
    public function editAction()
    {
    	$id = (int) $this->params()->fromRoute('id', 0);
    	if (!$id) {
    		return $this->redirect()->toRoute('opportunity', array(
    				'action' => 'edit'
    		));
    	}
    
    	try {
    		$opportunity = $this->getOpportunityTable()->getOpportunity($id);
    		
    	}
    	catch (\Exception $ex) {
    		return $this->redirect()->toRoute('opportunity', array(
    				'action' => 'index'
    		));
    	}
    
    	$form  = new OpportunityForm();
    	$form->bind($opportunity);
    	$form->get('submit')->setAttribute('value', 'Edit');
    
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		$form->setInputFilter($opportunity->getInputFilter());
    		$form->setData($request->getPost());
    
    		if ($form->isValid()) {
    			$this->getOpportunityTable()->saveOpportunity($opportunity);
    
    			// Redirect to list of albums
    			return $this->redirect()->toRoute('opportunity');
    		}
    	}
    
    	return array(
    			'id' => $id,
    			'form' => $form,
    	);
    }
    
    public function deleteAction()
    {
    	$id = (int) $this->params()->fromRoute('id', 0);
    	if (!$id) {
    		return $this->redirect()->toRoute('opportunity');
    	}
    
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		$del = $request->getPost('del', 'No');
    
    		if ($del == 'Yes') {
    			$id = (int) $request->getPost('id');
    			$this->getOpportunityTable()->deleteOpportunity($id);
    		}
    
    		// Redirect to list of albums
    		return $this->redirect()->toRoute('opportunity');
    	}
    
    	return array(
    			'id'    => $id,
    			'opportunity' => $this->getOpportunityTable()->getOpportunity($id)
    	);
    }
    
    public function detailAction()
    {
    	$id = (int) $this->params()->fromRoute('id', 0);
    	if (!$id) {
    		return $this->redirect()->toRoute('opportunity', array(
    				'action' => 'index'
    		));
    	}
    
    	// Get the Album with the specified id.  An exception is thrown
    	// if it cannot be found, in which case go to the index page.
    	try {
    		$opportunity = $this->getDetailTable()->getOpportunity($id);
    	}
    	catch (\Exception $ex) {
    		return $this->redirect()->toRoute('opportunity', array(
    				'action' => 'index'
    		));
    	}
    
    	$form  = new DetailForm();
    	$form->bind($opportunity);
    		
    	return array(
    			'id' => $id,
    			'form' => $form,
    	);
    }
    
    public function getHomeTable()
    {
    	if (!$this->homeTable) {
    		$sm = $this->getServiceLocator();
            $this->homeTable = $sm->get('Opportunity\Model\HomeTable');
            }
        return $this->homeTable;
    }
    
    public function getOpportunityTable()
    {
     	if (!$this->opportunityTable) {
    		$sm = $this->getServiceLocator();
            $this->opportunityTable = $sm->get('Opportunity\Model\OpportunityTable');
            }
        return $this->opportunityTable;
    }

    public function getDetailTable()
    {
    	if (!$this->detailTable) {
    		$sm = $this->getServiceLocator();
            $this->detailTable = $sm->get('Opportunity\Model\DetailTable');
            }
        return $this->detailTable;
    }
}